<template>
  <div class="home-page" v-show="bol">
    <van-tabs v-model="activeIndex" animated>
      <van-tab
        v-for="item in list"
        :key="item.id"
        :title="item.name"
        :name="item.id"
      >
        <ArticleList :id="item.id"></ArticleList>
        <!-- <article-list></article-list> -->
      </van-tab>
    </van-tabs>
    <!-- 按钮 -->
    <div class="btn-wrapper">
      <geek-icon class="geek-icon" name="search"></geek-icon>
      <geek-icon
        class="geek-icon"
        name="channel"
        @click.native="iconClick"
      ></geek-icon>
    </div>
    <!-- 弹窗 -->

    <ArticleChannel
      :activeIndex.sync="activeIndex"
      :list="list"
      ref="ArticleChannel"
    ></ArticleChannel>
  </div>
</template>

<script>
import ArticleList from './components/article-list.vue'
// import ArticleList from './components/article-list.vue'
import { channels } from '@/api/home.js'
import ArticleChannel from './components/article-channel.vue'
export default {
  name: 'HomePage',
  components: { ArticleList, ArticleChannel },
  data () {
    return {
      activeIndex: 0,
      list: [],
      bol: true
    }
  },
  watch: {
    // 侦听token值的变化,判断用户是否登录,登录状态改变立刻调用接口,更新数据
    '$store.state.user.token': {
      async handler (newVal) {
        // newVal存在,说明登录状态发生改变
        if (newVal) {
          // 将list还原,用户默认频道
          // this.list = []
          this.bol = false
          const res = await channels()
          this.list = res
          this.activeIndex = this.list[0].id
          this.bol = true
        }
      }
    }
  },
  async created () {
    const res = await channels()
    this.list = res
    this.activeIndex = this.list[0].id

    // console.log(this.list)
  },
  methods: {
    iconClick () {
      this.$refs.ArticleChannel.isShow = true
    }
  }
}
</script>
<style lang="less" scoped>
::v-deep .van-tabs {
  height: 100%;
  display: flex;
  flex-direction: column;
  .van-tabs__line {
    background: @geek-color;
    height: 2px;
    width: 32px;
  }
  .van-tab {
    color: #9ea1ae;
  }
  .van-tab--active {
    font-size: 18px;
    color: #333;
  }
  .van-tabs__wrap {
    padding-right: 86px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  }
  .van-tabs__content {
    flex: 1;
    overflow: hidden;
  }
  .van-tab__pane {
    height: 100%;
  }
}
.home-page {
  .btn-wrapper {
    position: absolute;
    right: 0;
    top: 0;
    width: 86px;
    height: 44px;
    background: #fff;
    display: flex;
    align-items: center;
    .geek-icon {
      flex: 1;
      text-align: center;
      font-size: 18px;
    }
    &::before {
      content: '';
      width: 20px;
      height: 44px;
      position: absolute;
      left: -20px;
      top: 0;
      background: linear-gradient(to right, rgba(255, 255, 255, 0), #fff);
    }
  }
}
</style>
